package indv.Cshen.cfengsecurityshirodemo.controller;

import indv.Cshen.cfengsecurityshirodemo.domain.ShiroUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Cfeng
 * @date 2022/7/19
 * 这个loginController主要就是能够进行登录的验证
 */

@RestController
@RequestMapping("/common")
@Slf4j
public class LoginController {

    @PostMapping("/login")
    //属性注入
    public Object login(ShiroUser user) {
//        System.out.println(user);
        Map<String,String> errorMsg = new HashMap<>();
        //获取当前的登录的主体
        Subject currentUser = SecurityUtils.getSubject();
        //登录当前主体，这里的登录需要使用Realm【Realm就是访问数据源的Dao】
        //获得当前用户的token
        if(!currentUser.isAuthenticated() && !currentUser.isRemembered()) {
            //没有认证，认证
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getUserPwd());
            //这里的用户令牌设置为RememberMe，这就符合@RequiresUser，只是获取令牌为@RequiresAuthentication
            token.setRememberMe(true);
            //登录当前主体
            try {
                currentUser.login(token);
                currentUser.getSession().setAttribute("currentUser",currentUser.getPrincipal());
                return "login succeed";
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of" + token.getPrincipal());
                errorMsg.put("errorMsg","用户不存在");
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account" + token.getPrincipal() + "was incorrect");
                errorMsg.put("errorMsg","密码不正确");
            } catch (LockedAccountException lae) {
                log.info("the account for username" + token.getPrincipal() + "is locked");
                errorMsg.put("errorMsg","账户锁定");
            }
            catch (AuthenticationException e) {
                log.info("登录失败",e);
                errorMsg.put("errorMsg","登录失败");
            }
            return errorMsg;
        } else {
            //已经登录
            return  "already login";
        }
    }

    @GetMapping("/toLogin")
    public String test() {
        return "你好，CHINE";
    }

    @GetMapping("/getCurrentUser")
    public  ShiroUser getCurrentUser() {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return (ShiroUser) session.getAttribute("currentUser");
    }

//    @GetMapping("/logout"),由系统完成，配置了的
//    public void logout() {
//        Subject currentUser = SecurityUtils.getSubject();
//        //登出，销毁身份
//        currentUser.logout();
//    }

    @RequestMapping("/unauthrized")
    public String unauthrized() {
        return "unauthrized";
    }
}
